package com.yu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yu.controller.Code;
import com.yu.entity.User;
import com.yu.exception.BusinessException;
import com.yu.mapper.UserMapper;
import com.yu.service.AdmainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdmainServiceImpl implements AdmainService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User selectOne(User user) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper();
        lqw.eq(User::getUsername,user.getUsername());
        lqw.eq(User::getPassword,user.getPassword());
        User one = userMapper.selectOne(lqw);

        return one;
    }

    @Override
    public List<User> selectAll() {
        List<User> users = userMapper.selectList(null);
        return users;
    }

    @Override
    public Boolean save(User user) {
        int insert = userMapper.insert(user);
        return insert > 0;
    }

    @Override
    public Boolean deleteOne(Integer id) {
        int i = userMapper.deleteById(id);
        return i > 0;
    }

    @Override
    public User selectOneById(Integer id) {
        User user = userMapper.selectById(id);
        return user;
    }

    @Override
    public Boolean updateOne(User user) {
        int i = userMapper.update(user, null);
        return i > 0;
    }

    @Override
    public Boolean register(User user) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper();
        lqw.eq(User::getUsername,user.getUsername());
        User one = userMapper.selectOne(lqw);
        if(one != null) {
            throw new BusinessException(Code.BUSINESS_ERR,"该管理员名已存在，请重新输入");
        }
        int i = userMapper.insert(user);
        return i > 0;
    }


}
